package com.gui.collection.list;

import com.gui.utils.Sleeper;

import java.util.ArrayList;
import java.util.List;

public class FailFastDemo {

    public static void main(String[] args) {

        /*
            failFast 发现在遍历的同时，有人在修改数据，则立马抛出异常
         */

        List<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");
        Thread thread = new Thread(() -> {
            Sleeper.sleep(1);
            list.add("F");
        });
        thread.start();
        for (int i = 0; i < list.size(); i++) {
            String ele = list.get(i);
            System.out.println(ele);
            if ("B".equals(ele)) {
                Sleeper.sleep(2);
            }
        }

    }
}
